package com.atguigu.ssyx.activity.controller;


import com.atguigu.ssyx.activity.service.CouponInfoService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.activity.CouponInfo;
import com.atguigu.ssyx.vo.activity.CouponRuleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠券信息 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-09-17
 */

@Api(tags = "优惠劵管理")
@RestController
@RequestMapping("/admin/activity/couponInfo")
public class CouponInfoController {

    @Autowired
    private CouponInfoService couponInfoService;


    //1.优惠劵的分页查询
    @ApiOperation("优惠劵的分页查询")
    @GetMapping("/{page}/{limit}")

    public Result list(@PathVariable("page") Long page,
                        @PathVariable("limit") Long limit) {
      Page<CouponInfo>  pagegaram =new Page(page, limit);
       IPage<CouponInfo> pageModel= couponInfoService.selectPage(pagegaram);
       return Result.ok(pageModel);
    }


    //2.添加优惠劵
    @ApiOperation("添加优惠劵")
    @PostMapping("/save")
    public Result save(@RequestBody CouponInfo couponInfo) {
        couponInfoService.save(couponInfo);
        return Result.ok(null);
    }

    //3.根据id查询优惠劵
    @ApiOperation("根据id查询优惠劵")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") Long id) {
        CouponInfo couponInfo = couponInfoService.getCouponInfoById(id);
        return Result.ok(couponInfo);
    }

    //4.根据优惠劵的id查询规则数据
//    url: `${api_name}/findCouponRuleList/${id}`,
//    method: 'get'
    @ApiOperation("根据优惠劵的id查询规则数据")
    @GetMapping("findCouponRuleList/{id}")
    public Result findCouponRuleList(@PathVariable("id") Long id) {
       Map<String,Object> reultMap= couponInfoService.findCouponRuleList(id);
        return Result.ok(reultMap);
    }
    //5.添加优惠劵的规则数据
    @ApiOperation("添加优惠劵的规则数据")
    @PostMapping("/saveCouponRule")
    public Result saveCouponRule(@RequestBody CouponRuleVo couponRuleVo) {
        couponInfoService.saveCouponRule(couponRuleVo);
        return Result.ok(null);
    }

    //6.更新优惠劵
    @ApiOperation("更新优惠劵")
    @PutMapping("/update")
    public Result update(@RequestBody CouponInfo couponInfo) {
        couponInfoService.updateById(couponInfo);
        return Result.ok(null);
    }

    //7.根据id删除优惠劵
    @ApiOperation("根据id删除优惠劵")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") Long id) {
        couponInfoService.removeById(id);
        return Result.ok(null);
    }

    //8.批量删除
    @ApiOperation("批量删除优惠劵")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        couponInfoService.removeByIds(idList);
        return Result.ok(null);
    }

    //9.根据关键字来查询他匹配的优惠劵信息
    @ApiOperation("根据关键字来查询他匹配的优惠劵信息")
    @GetMapping("/findCouponByKeyword/{keyword}")
    public Result findCouponByKeyword(@PathVariable("keyword") String keyword) {
       List<CouponInfo> list= couponInfoService.findCouponByKeyword(keyword);
       return Result.ok(list);
    }
}

